<template>
  <div class="ma-content-block">
    <!-- Tab 栏 jia-->
    <a-tabs v-model="activeTab" @change="handleTabChange" v-if="false">
      <a-tab-pane key="zh" title="中文"></a-tab-pane>
      <a-tab-pane key="en" title="英文"></a-tab-pane>
      <a-tab-pane key="ja" title="日语"></a-tab-pane>
      <a-tab-pane key="ko" title="韩语"></a-tab-pane>
    </a-tabs>

    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="金融分类" field="finance_category">
            <a-select
                v-model="searchForm.finance_category"
                :options="postData"
                allow-clear
                placeholder="请选择金融分类" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="金融机构" field="finance_institution">
            <a-select
                v-model="searchForm.finance_institution"
                :options="postDatacate"
                allow-clear
                placeholder="请选择金融机构" />
          </a-form-item>
        </a-col>
<!--        <a-col :span="8">-->
<!--          <a-form-item label="开始时间" field="start_time">-->
<!--            <a-range-picker v-model="searchForm.start_time" :show-time="true" mode="date" />-->
<!--          </a-form-item>-->
<!--        </a-col>-->
        <a-col :span="8">
          <a-form-item label="结束时间" field="end_time">
            <a-range-picker v-model="searchForm.end_time" :show-time="true" mode="date" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="展示/下架" field="is_display">
            <sa-select v-model="searchForm.is_display" dict="yes_or_no" placeholder="请选择是否展示/下架" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="金融标题" field="finance_title">
            <a-input v-model="searchForm.finance_title" placeholder="请输入金融标题" />
          </a-form-item>
        </a-col>
      </template>
      <template #sort_order="{ record }">
        <a-input-number
            v-model="record.sort_order"
            :min="0"
            style="width: 80px"
            @change="changeSortOrder(record.finance_id, record.sort_order)"
        />
      </template>
      <!-- Table 自定义渲染 -->
      <template #is_display="{ record }">
        <sa-switch v-model="record.is_display" checkedText="是"  uncheckedText="否" @change="changeStatus($event, record.finance_id,1)"></sa-switch>
      </template>
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import EditForm from './edit.vue'
import api from '../api/finance'
import commonApi from '@/api/common'
// 引用定义
const crudRef = ref()
const editRef = ref()
const activeTab = ref('zh') // 默认选中中文jia
const postData = ref([])
const postDatacate = ref([])

// 搜索表单
const searchForm = ref({
  finance_category: '',
  finance_institution: '',
  start_time: [],
  end_time: [],
  is_display: '',
  finance_title: '',
  lang_code:''
})


// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  pk: 'finance_id',
  rowSelection: { showCheckedAll: true },
  add: {
    show: true,
    auth: ['/finance/finance/save'],
    func: async (params) => {
      const currentLangCode = searchForm.value.lang_code;
      editRef.value?.open()
      editRef.value?.setFormData({lang_code:currentLangCode})
    },
  },
  edit: {
    show: true,
    auth: ['/finance/finance/update'],
    func: async (record) => {
      editRef.value?.open('edit',record.finance_id)
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/finance/finance/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
  },
   export: { show: true, url: '/finance/finance/export', auth: ['/finance/finance/export'] },
})

// SaTable 列配置
const columns = reactive([
  { title: '排序', dataIndex: 'sort_order', width: 180 },
  { title: '金融标题', dataIndex: 'finance_title', width: 180 },
  { title: '金融分类', dataIndex: 'finance_category_text', width: 180 },
  { title: '金融机构', dataIndex: 'finance_institution_text', width: 180 },
  { title: '开始时间', dataIndex: 'start_time', width: 180 },
  { title: '结束时间', dataIndex: 'end_time', width: 180 },
  { title: '展示/下架', dataIndex: 'is_display',dict: 'yes_or_no', width: 180 },

  { title: '更新时间', dataIndex: 'update_time', width: 180 },
  
])

//jia
const changeStatus = async (status, id,type) => {
  const response = await api.changeStatus({ id, status,type})
  if (response.code === 200) {
    Message.success(response.message)
    crudRef.value.refresh()
  }
}
// 页面数据初始化
const initPage = async () => {
  searchForm.value.lang_code='zh'

  const postResp = await api.getcate({classification_type:5})

  postData.value = postResp.data

  const postRespcte = await commonApi.commonGet('/financeInstitution/financeInstitution/getSelect')

  postDatacate.value = postRespcte.data
}

//jia
const handleTabChange = async (key) => {
  activeTab.value = key
  // 设置语言代码
  searchForm.value.lang_code = key
  // 刷新表格数据
  crudRef.value?.refresh()
}
// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}
//jia
const changeSortOrder = async (id,sortOrder) => {
  try {
    const response = await api.updateSortOrder({
      id: id,
      sort_order: sortOrder
    });
    console.log(response,"fffffffffffffffffff")
    if (response.code === 200) {
      Message.success('排序修改成功');
      crudRef.value?.refresh();
    } else {
      Message.error('排序修改失败');
    }
  } catch (error) {
    console.log(error,"error")
    Message.error('排序修改失败1');
  }
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
